package com.ylbh.modules.yyt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.ylbh.common.pojo.goods.dto.GoodsDTO;
import com.ylbh.common.pojo.goods.dto.GoodsImgDTO;
import com.ylbh.common.pojo.goods.dto.SpecPropertyDTO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.accessory.GoodsInventoryDetails;
import com.ylbh.modules.goods.service.GoodsSpecService;
import com.ylbh.modules.goods.service.ShoppingGoodsService;
import com.ylbh.modules.yyt.entity.Sku;
import com.ylbh.modules.yyt.entity.SkuInventory;
import com.ylbh.modules.yyt.entity.SkuPrice;
import com.ylbh.modules.yyt.entity.SpuDetail;
import net.sf.json.JSONArray;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;

/**
 * @author: czx
 * @date: 2019/12/24 15:17
 */
@RestController
public class Test2 {

    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private ShoppingGoodsService shoppingGoodsService;

    public static String appKey = "yd8f7d0b54efeaa5f8";

    public static String appSecret = "850ebca5a67a49e19c3dbe23e9631347.";

    public static String baseUrl = "http://pre-openapi.380star.com";


    @GetMapping("/addYYT")
    public CommonResult addYYT() {
        System.out.println(System.currentTimeMillis());
        HttpClient httpClient = null;
        HttpResponse response = null;
        try {
        httpClient = HttpClients.createDefault();
        // 准备参数
        List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("currentTime",System.currentTimeMillis()+ ""));
            params.add(new BasicNameValuePair("appKey",appKey));
            params.add(new BasicNameValuePair("appSecret",appSecret));
            params.add(new BasicNameValuePair("pageIndex","10"));
            params.add(new BasicNameValuePair("pageSize","10"));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"UTF-8");
                //通过post方式访问
                HttpPost post = new HttpPost(baseUrl + "/goods/getSpuIdList");
                formEntity.setContentType("application/x-www-form-urlencoded");
        post.setEntity(formEntity);
        response = httpClient.execute(post);
        HttpEntity valueEntity = response.getEntity();
        String content = EntityUtils.toString(valueEntity);
            System.out.println(content);
        } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        } catch (ClientProtocolException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return Ref.ok();
    }

    /**
     * 设置接口基础参数
     * @param params
     * @return
     */
    private static Map<String, String> setBaseParams(Map<String, String> params){
        params.put("appKey", appKey);
        params.put("currentTime", System.currentTimeMillis() + "");
        String sign = getSign(params, appSecret);
        params.put("sign", sign);
        System.out.println(sign);
        return params;
    }
    /**
     * 获取签名
     * @param params
     * @param appSecret
     * @return
     */
    public static String getSign(Map<String, String> params, String appSecret) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!entry.getKey().equals("sign")) {
                // 拼接参数值字符串并进行utf-8解码，防止中文乱码产生
                String value = "";
                try {
                    value = URLDecoder.decode(entry.getValue(), "UTF-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (StringUtils.isEmpty(value)) {
                    continue;
                }
                params.put(entry.getKey(), value);
            }
        }
        // 将参数以参数名的字典升序排序
        Map<String, String> sortParams = new TreeMap<>(params);
        Set<Map.Entry<String, String>> entrys = sortParams.entrySet();
        // 遍历排序的字典,并拼接格式
        StringBuilder valueSb = new StringBuilder();
        for (Map.Entry<String, String> entry : entrys) {
            if (org.apache.commons.lang.StringUtils.isNotBlank(entry.getValue())) {
                valueSb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        valueSb.append(appSecret);
        String sign = valueSb.toString();
        try {
            sign = DigestUtils.md5Hex(sign.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return sign;
    }
}
